﻿package classes
{
	import classes.GameMenuItem;
	import classes.events.MenuItemEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.display.MovieClip;
	import flash.events.EventDispatcher;
	/**
	 * ...
	 * @author ...
	 */
	public class GameMenu extends MovieClip 
	{
		private var menuItems:Array = new Array();
		private var mainStage:CaveCraftGame;
		
		public function GameMenu(gameStage:CaveCraftGame):void
		{
			this.mainStage = gameStage;
			createItems();
			addEventListeners();
		}
		
		private function createItems():void 
		{
			this.menuItems.push(new GameMenuItem('menu1'));
			this.menuItems.push(new GameMenuItem('menu2'));
			this.menuItems.push(new GameMenuItem('menu3'));
			this.menuItems.push(new GameMenuItem('menu4'));
		}
		
		private function addEventListeners():void 
		{
			for each (var item:GameMenuItem in menuItems) 
			{
				item.addEventListener(MouseEvent.CLICK,handleMenuClick);
			}
		}
		
		private function removeAllEventListeners():void 
		{
			for each (var item:GameMenuItem in menuItems) 
			{
				item.removeEventListener(MenuItemEvent.MENU_CLICK_EVENT,handleMenuClick);
			}
		}
		
		public function handleMenuClick(e:MouseEvent):void
		{
			var menuItem:GameMenuItem = GameMenuItem(e.currentTarget);
			trace(menuItem.getText());
			dispatchEvent(new MenuItemEvent(MenuItemEvent.MENU_CLICK_EVENT,menuItem.getText()));
		}
		
		public function showMenu():void 
		{
			mainStage.addChild(this);
			var y = 0;
			for each (var item:GameMenuItem in menuItems) 
			{
				item.y = y;
				this.addChild(item);
				y += 20;
			}
		}
		
		private function removeAllChildren(): void 
		{
			
		}
		
		public function hideMenu():void
		{
			this.removeAllEventListeners();
			
			dispatchEvent(new Event('exit'));
		}
	}
	
}